package com.java.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.java.common.enums.CommentLevel;
import com.java.common.exception.BusinessExceptionEnum;
import com.java.common.exception.RRException;
import com.java.common.utils.DesensitizationUtil;
import com.java.common.utils.PageUtils;
import com.java.common.utils.Query;
import com.java.mapper.ItemsDao;
import com.java.pojo.*;
import com.java.pojo.vo.CommentLevelCountsVO;
import com.java.pojo.vo.ItemCommentVO;
import com.java.pojo.vo.SearchItemsVO;
import com.java.pojo.vo.ShopcartVO;
import com.java.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("itemsService")
public class ItemsServiceImpl extends ServiceImpl<ItemsDao, Items> implements ItemsService {
    @Autowired
    private ItemsImgService imgService;
    @Autowired
    private ItemsParamService paramService;
    @Autowired
    private ItemsSpecService specService;
    @Autowired
    private ItemsCommentsService commentsService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<Items> page = this.page(
                new Query<Items>().getPage(params),
                // 拼接查询条件
                new LambdaQueryWrapper<Items>()
        );

        return new PageUtils(page);
    }

    @Override
    public Items queryItemById(String id) {
        return this.baseMapper.selectById(id);
    }

    @Override
    public List<ItemsImg> queryItemImgList(String itemId) {
        return imgService.list(new LambdaQueryWrapper<ItemsImg>().eq(ItemsImg::getItemId, itemId));
    }

    @Override
    public List<ItemsSpec> queryItemSpecList(String itemId) {
        return specService.list(new LambdaQueryWrapper<ItemsSpec>().eq(ItemsSpec::getItemId, itemId));
    }

    @Override
    public ItemsParam queryItemParam(String itemId) {
        return paramService.getOne(new LambdaQueryWrapper<ItemsParam>().eq(ItemsParam::getItemId, itemId));
    }

    @Override
    public CommentLevelCountsVO queryCommentCounts(String itemId) {
        List<ItemsComments> comments = commentsService.list(new LambdaQueryWrapper<ItemsComments>().eq(ItemsComments::getItemId, itemId));
        long goodCounts = comments.stream().filter(t -> t.getCommentLevel().equals(CommentLevel.GOOD.type)).count();
        long normalCounts = comments.stream().filter(t -> t.getCommentLevel().equals(CommentLevel.NORMAL.type)).count();
        long badCounts = comments.stream().filter(t -> t.getCommentLevel().equals(CommentLevel.BAD.type)).count();
        int totalCounts = comments.size();

        CommentLevelCountsVO commentLevelCountsVO = new CommentLevelCountsVO();
        commentLevelCountsVO.setTotalCounts(totalCounts);
        commentLevelCountsVO.setGoodCounts(goodCounts);
        commentLevelCountsVO.setNormalCounts(normalCounts);
        commentLevelCountsVO.setBadCounts(badCounts);
        return commentLevelCountsVO;
    }

    @Override
    public PageUtils queryPageItemComments(Integer pageNo, Integer size, Integer commentLevel, String itemId) {
        Page<ItemCommentVO> page = new Page<>(pageNo, size);
        List<ItemCommentVO> list = this.baseMapper.queryItemComments(page, commentLevel, itemId);
        // 脱敏
        for (ItemCommentVO itemCommentVO : list) {
            itemCommentVO.setNickname(DesensitizationUtil.commonDisplay(itemCommentVO.getNickname()));
        }
        page.setRecords(list);
        return new PageUtils(page);
    }

    @Override
    public PageUtils searchItems(String keyword, String sort, Integer pageNo, Integer size) {
        Map<String, Object> params = new HashMap<>();
        params.put("keyword", keyword);
        params.put("sort", sort);
        Page<SearchItemsVO> page = new Page<>(pageNo, size);
        List<SearchItemsVO> list = this.baseMapper.searchItemsVo(page, params);
        page.setRecords(list);
        return new PageUtils(page);
    }

    @Override
    public PageUtils searchItems(Integer catId, String sort, Integer pageNo, Integer size) {
        Map<String, Object> params = new HashMap<>();
        params.put("catId", catId);
        params.put("sort", sort);
        Page<SearchItemsVO> page = new Page<>(pageNo, size);
        List<SearchItemsVO> list = this.baseMapper.searchItemsVoByThirdCatId(page, params);
        page.setRecords(list);
        return new PageUtils(page);
    }

    @Override
    public List<ShopcartVO> queryItemsBySpecIds(String specIds) {
        String[] split = specIds.split(",");
        List<String> ids = Arrays.asList(split);
        return this.baseMapper.queryItemsBySpecIds(ids);
    }

    @Override
    @Transactional
    public void decreaseItemSpecStock(String specId, Integer buyCounts) {
        int i = this.baseMapper.decreaseItemSpecStock(specId, buyCounts);
        if (i != 1) {
            throw new RRException(BusinessExceptionEnum.STOCK_NOT_ENOUGH.getDesc());
        }
    }

}